/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.anniversary.service.impl;

import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.anniversary.domain.AppUserInfo;
import me.zhengjie.modules.anniversary.domain.AssociationUserInfo;
import me.zhengjie.modules.anniversary.repository.AppUserInfoRepository;
import me.zhengjie.modules.anniversary.service.AppUserInfoService;
import me.zhengjie.modules.anniversary.service.dto.AppUserInfoDto;
import me.zhengjie.modules.anniversary.service.dto.AppUserInfoQueryCriteria;
import me.zhengjie.modules.anniversary.service.mapstruct.AppUserInfoMapper;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.ValidationUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author Jiayin Wei
* @date 2020-07-28
**/
@Service
@RequiredArgsConstructor
public class AppUserInfoServiceImpl implements AppUserInfoService {

    private final AppUserInfoRepository appUserInfoRepository;
    private final AppUserInfoMapper appUserInfoMapper;

    @Override
    public Map<String,Object> queryAll(AppUserInfoQueryCriteria criteria, Pageable pageable){
        Page<AppUserInfo> page = appUserInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(appUserInfoMapper::toDto));
    }

    @Override
    public List<AppUserInfoDto> queryAll(AppUserInfoQueryCriteria criteria){
        return appUserInfoMapper.toDto(appUserInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public AppUserInfoDto findById(Long id) {
        AppUserInfo appUserInfo = appUserInfoRepository.findById(id).orElseGet(AppUserInfo::new);
        ValidationUtil.isNull(appUserInfo.getId(),"AppUserInfo","id",id);
        return appUserInfoMapper.toDto(appUserInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AppUserInfoDto create(AppUserInfo resources) {
        return appUserInfoMapper.toDto(appUserInfoRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AppUserInfo resources) {
        AppUserInfo appUserInfo = appUserInfoRepository.findById(resources.getId()).orElseGet(AppUserInfo::new);
        ValidationUtil.isNull( appUserInfo.getId(),"AppUserInfo","id",resources.getId());
        appUserInfo.copy(resources);
        appUserInfoRepository.save(appUserInfo);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            appUserInfoRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<AppUserInfoDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AppUserInfoDto appUserInfo : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("名称", appUserInfo.getName());
            map.put("校庆活动ID", appUserInfo.getActivitiesId());
            map.put("预约人员ID", appUserInfo.getUserId());
            map.put("预约人员类型（0：学生校友，1：教职工校友）", appUserInfo.getType());
            map.put("创建者名称", appUserInfo.getCreateBy());
            map.put("创建时间", appUserInfo.getCreateTime());
            map.put("修改者名称", appUserInfo.getUpdatedBy());
            map.put("修改时间", appUserInfo.getUpdateTime());
            map.put("排序", appUserInfo.getSort());
            map.put("备注", appUserInfo.getRemark());
            map.put("是否启用", appUserInfo.getIsEnable());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public AppUserInfo queryByIdAndUserId(Long id, Long userId) {
        return appUserInfoRepository.queryByIdAndUserId(id, userId);
    }
}
